#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       yodawg.py
#       
#       Copyright 2011 www.soycode.com
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 3 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

"""
Arbitrary depth Yo Dawg yoyo/dog image and text generator.
www.soycode.com
"""

# standard
import sys
import textwrap
# installed
from PIL import Image, ImageDraw, ImageFont
# package

TEXT_BASE = ['Yo dawg, we herd you like yo-yos and', '',
             'so we put the', '',
             'in a yo-yo so you can yo-yo yo\'', '',
             'while you yo-yo yo\'', '',
             'while yo\'', '',
             'yo-yos, dawg.']


# This or a similar class may be useful to expand flexibility.
#class YoDawg:
#  """Object to reflect a certain depth of Yo Dawg."""
#  def __init__(self, depth, clarify, obfuscate):
#    self.text = GenerateText(depth, clarify, obfuscate)


def GenerateImage(depth, clarify, obfuscate):
  """Generate the Yo Dawg image to a certain depth."""
  original = Image.open('source_images/original.jpg')
  yoyo = Image.open('source_images/yoyo.png')
  font = ImageFont.load_default()
  inner = original.resize((300, 150), Image.ANTIALIAS)
  for i in range(depth + 1):
    outter = yoyo.copy()
    text = GenerateText(i, clarify, obfuscate)
    lines = textwrap.wrap(text, 90)
    outter.paste(inner, (200, 150))
    draw = ImageDraw.Draw(outter)
    height = font.getsize(text)[1]
    line_counter = 0
    for line in lines:
      draw.text((625, 0 + (line_counter * height)), line, font=font,
                fill=(0, 0, 0))
      line_counter += 1
    inner = outter.resize((300, 150), Image.ANTIALIAS)
  return outter


def GenerateText(depth, clarify, obfuscate):
  """Generate the Yo Dawg text to a certain depth."""
  text = list(TEXT_BASE) # *copy* base text list
  if not clarify:
    text[1] = ('yo-yo Yo Dawg ' * depth + 'Yo Dawgs') # prior level meme liked
    text[3] = ('yo-yo Yo Dawg ' * (depth + 1)).strip() # name of this Yo Dawg
    text[5] = ('yo-yo Yo Dawg ' * (depth + 1)).strip() # same thing
    text[7] = 'Yo Dawg yo-yo ' * (depth + 2) + 'dawg' # dog in the Yo-Yos
    text[9] = ('yo-yo Yo Dawg ' * (depth + 1) + 'yo-yo dawg') # dog in memes
  else:
    text[1] = ('an Xzibit meme featuring a yo-yo toy about ' * depth +
               'the Xzibit meme')
    text[3] = ('Xzibit meme featuring a yo-yo ' * (depth + 1)).strip()
    text[5] = ('Xzibit meme featuring a yo-yo ' * (depth + 1)).strip()
    text[7] = ('dog featured on an ' +
               ('Xzibit meme yo-yo on an ' * (depth + 1))
               + 'Xzibit meme yo-yo')
    text[9] = ('dog on a yo-yo ' + 
               ('in an Xzibit meme about yo-yos ' * (depth + 1)).strip())
  result = ' '.join(text)
  if obfuscate:
    result = result.replace('-', ' ').replace('\'', '').replace(
							'you', 'yo').upper()
  return result


def main():
  args = sys.argv[1:]

  if not args:
    print 'Usage: --depth X [--image] [--clarify] [--obfuscate]'
    print 'The last three arguments are optional, assumed false.'
    print 'image: generate image file (default returns text only)'
    print 'clarify: make the text clearer by substituting words'
    print 'obfuscate: capitalize all text and remove most punctuation'
    sys.exit(0)

  # parse command line args
  gotdepth = False
  depth = False
  image = False
  clarify = False
  obfuscate = False
  for arg in args:
    if gotdepth:
      depth = arg
      gotdepth = False
    elif arg == '--depth':
      gotdepth = True
    elif arg == '--image':
      image = True
    elif arg == '--clarify':
      clarify = True
    elif arg == '--obfuscate':
      obfuscate = True
    else:
      print 'Error! Run without arguments to see proper usage.'
      sys.exit(1)

  if not depth:
    print 'Error! Unspecified depth.'
    sys.exit(1)
  try:
    depth = int(depth)
  except:
    print 'Error! Depth is not an integer.'
    sys.exit(1)

  if not image:
    print GenerateText(depth, clarify, obfuscate)
  else:
    image = GenerateImage(depth, clarify, obfuscate)
    image.save('yodawg_depth%s.png' % depth)


if __name__ == "__main__":
  main()

